package panels;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.JCheckBox;

import engines.GoogleEngine;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;



public class MainPanel extends JPanel implements Runnable {
	private JTextField addressField;
	private JTextField textField_1;
	private JTextArea textArea;

        boolean toc = false;

	JCheckBox chckbxYes = new JCheckBox("Yes");
	/**
	 * Create the panel.
	 */
	public MainPanel() {


		setLayout(new BorderLayout(0, 0));

		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		add(toolBar, BorderLayout.NORTH);

		JPanel input = new JPanel();
		toolBar.add(input);
		input.setLayout(new GridLayout(2, 2, 0, 0));

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Title", TitledBorder.LEADING, TitledBorder.TOP, null, null), "Address", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		input.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		addressField = new JTextField();
		panel.add(addressField);
		addressField.setColumns(10);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "File Name", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		input.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));

		textField_1 = new JTextField();
		panel_1.add(textField_1);
		textField_1.setColumns(10);

		JPanel format = new JPanel();
		toolBar.add(format);
		format.setLayout(new GridLayout(2, 1, 0, 0));

		JPanel panel_2 = new JPanel();
		format.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 1, 0, 0));

		JComboBox<Object> comboBox = new JComboBox<Object>();
		panel_2.add(comboBox);
		comboBox.setModel(new DefaultComboBoxModel<Object>(new String[] {"epub", "fb2", "oeb", "lit", "lrf", "mobi", "pdb", "pml", "rb", "pdf", "rtf", "tcr", "txt"}));
		comboBox.setMaximumRowCount(13);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Using a TOC?", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		format.add(panel_3);


		panel_3.add(chckbxYes);

		JPanel start = new JPanel();
		toolBar.add(start);
		start.setLayout(new BoxLayout(start, BoxLayout.X_AXIS));

		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                begin();
              }
          });
		start.add(btnStart);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane,BorderLayout.CENTER);

		textArea = new JTextArea();
		textArea.setRows(20);
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setColumns(50);
		scrollPane.setViewportView(textArea);

	}


	private void begin() {
		new Thread(this).start();

	}

	public void run()
	{
		String source = addressField.getText();;

		if(source.contains("https://docs.google.com/document/d/") && !chckbxYes.isSelected()) //Add TOC boolean to another method that is similar
		{
			GoogleEngine goog = new GoogleEngine();
			goog.setLink(addressField.getText());
			goog.parseForID();
			goog.getChapter();
                        goog.addChapterContents();
                        goog.writeChapters();

                        goog.osCommand("ebook-convert 0.html out.epub");
			goog.cleanUp();
		}

                if(source.contains("https://docs.google.com/document/d/")&& this.chckbxYes.isSelected()) //Add TOC boolean to another method that is similar
		{
			GoogleEngine goog = new GoogleEngine();
			goog.setLink(addressField.getText());
			goog.parseForID();
			goog.getChapter();

                        goog.populateTOC();

                        int i = 0;
                        int stop = goog.chapterLinkCount();

                        while(i < stop)
                        {
                            System.out.println("Getting chapter " + String.valueOf(i) + " out of " + String.valueOf(stop));
                            goog.setLink(goog.getChapterLink(i));
                            goog.parseForID();
                            goog.getChapter();
                            goog.addChapterContents();
                            //goog.addChapterTitle();

                            i++;
                        }


                        goog.writeChapters();

                        try
                        {
                            goog.createIndex();

                        } catch (FileNotFoundException ex)
                        {
                            Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    goog.osCommand("ebook-convert index.opf out.epub");
                    goog.cleanUp();
                    }



	}
}
